import dayjs from "dayjs";
import { DATE_TIME_FORMATTER } from "@chulei-ui/utils";
import {
  PLANT_BASIC_FACTORY_GROUP_ID,
  PLANT_CENTER_PIN_GROUP_ID,
  PLANT_COLLECTOR_GROUP_ID,
  PLANT_SEND_PIN_GROUP_ID,
  PLANT_TEXT,
} from "../../../constant";
import { useCacheStore } from "../../../sde/cache";
import { PlanetUtils } from "../utils/PlanetUtils";
import { PlanetSender } from "./PlanetSender";
import { PlanetFactory } from "./PlanetFactory";
import { PlanetCollector } from "./PlanetCollector";
import { PlanetProduct } from "./PlanetProduct";
import { PlanetStatistics } from "./PlanetStatistics";
import type { UniverseSystem, UniverseType } from "../../../sde/entity";
import type { PlanetLayout } from "@scvzerng/esi-163/dist/types/esi/plants/model/PlanetLayout";

export class PlayerPlanet {
  /**
   * 编号
   */
  id!: number;
  /**
   * 名称
   */
  name!: string;
  /**
   * 建筑数量
   */
  structCount!: number;
  /**
   * 指挥中心等级
   */
  level!: number;

  lastUpdate!: string;
  type!: UniverseType;
  player!: string;
  /**
   * 发射中心
   */
  launchCenter: PlanetSender[] = [];
  /**
   * 指挥中心
   */
  commandCenter?: any;
  /**
   * 加工厂
   */
  factories: PlanetFactory[] = [];
  /**
   * 采集器
   */
  collector: PlanetCollector[] = [];

  layout!: PlanetLayout;

  system!: UniverseSystem;

  static from(planet: any, layout: PlanetLayout, player: string): PlayerPlanet {
    const cacheStore = useCacheStore();
    const playerPlant = new PlayerPlanet();
    playerPlant.id = planet.planet_id;
    playerPlant.name = PLANT_TEXT[planet.planet_type];
    playerPlant.level = planet.upgrade_level;
    playerPlant.structCount = planet.num_pins;
    playerPlant.level = planet.upgrade_level;
    playerPlant.lastUpdate = dayjs(planet.last_update).format(DATE_TIME_FORMATTER);
    playerPlant.player = player;
    playerPlant.type = cacheStore.getType(playerPlant.name)!;
    layout.pins.forEach((item) => {
      const type = cacheStore.getType(item.type_id);
      //@ts-ignore
      item.type = type;
      item.contents?.forEach((content) => {
        //@ts-ignore
        content.type = cacheStore.getType(content.type_id);
      });
      if (type?.groupId === PLANT_CENTER_PIN_GROUP_ID) {
        playerPlant.commandCenter = item;
      }
      if (type?.groupId === PLANT_SEND_PIN_GROUP_ID) {
        playerPlant.launchCenter.push(new PlanetSender(item, type));
      }
      if (type?.groupId === PLANT_BASIC_FACTORY_GROUP_ID) {
        playerPlant.factories.push(
          new PlanetFactory(
            item,
            type,
            layout.routes.map((route) => ({
              ...route,
              type: cacheStore.getType(route.content_type_id)!,
            }))
          )
        );
      }
      if (type?.groupId === PLANT_COLLECTOR_GROUP_ID) {
        playerPlant.collector.push(
          new PlanetCollector(item, type, cacheStore.getType(item.extractor_details?.product_type_id!))
        );
      }
    });
    console.log(playerPlant, layout);
    layout.routes.forEach((item) => {
      //@ts-ignore
      item.type = cacheStore.getType(item.content_type_id);
    });
    playerPlant.system = cacheStore.universeElementMap[planet.solar_system_id]! as UniverseSystem;
    playerPlant.layout = layout;
    console.log(playerPlant);
    return playerPlant;
  }

  /**
   * 获取星球采集的所有基础资源
   */
  get basicResource() {
    return PlanetUtils.accumulatePlanetProduct(
      this.collector
        .filter((c) => c.fetchType)
        .map((c) => new PlanetProduct(c.totalFetchResource, c.fetchType!, c.standardPerCycleCount))
    );
  }

  /**
   * 获取当前行星的统计信息
   */
  get statistics() {
    return new PlanetStatistics(this);
  }

  /**
   * 发射中心添加清单
   */
  get launchCenterDetails() {
    if (this.launchCenter.length !== 1) {
      return [];
    }
    const center = this.launchCenter[0]!;
    center.calcDetails(this.statistics.totalInputStatistics);

    return center.calcDetails(this.statistics.totalInputStatistics);
  }
}
